package com.abso.sunlight.explorer.preferences;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * The general page displaying Congress Explorer preferences.
 */
public class GeneralPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {

    /** The checkbox used to choose if long-running operations must be in background. */
    private Button showUserDialogButton;


    @SuppressWarnings("restriction")
    @Override
    protected Control createContents(Composite parent) {
        Composite composite = createComposite(parent);
        showUserDialogButton = new Button(composite, SWT.CHECK);
        showUserDialogButton.setText("Run long operations in the background");
        showUserDialogButton.setToolTipText("Run long operations in the background where possible");
        showUserDialogButton.setSelection(getPreferenceStore().getBoolean(
                org.eclipse.ui.internal.IPreferenceConstants.RUN_IN_BACKGROUND));
        applyDialogFont(composite);
        return composite;
    }

  /**
     * Constructs a new composite.
     * 
     * @param parent
     *            the parent control.
     * @return
     */
    protected Composite createComposite(Composite parent) {
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        composite.setLayout(layout);
        composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
        return composite;
    }

    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
     */
    public void init(IWorkbench workbench) {
    }

    @Override
    @SuppressWarnings("restriction")
    protected void performDefaults() {
        IPreferenceStore store = getPreferenceStore();
        showUserDialogButton.setSelection(store.getDefaultBoolean(org.eclipse.ui.internal.IPreferenceConstants.RUN_IN_BACKGROUND));
        super.performDefaults();
    }

    @Override
    @SuppressWarnings("restriction")
    public boolean performOk() {
        IPreferenceStore store = getPreferenceStore();
        store.setValue(org.eclipse.ui.internal.IPreferenceConstants.RUN_IN_BACKGROUND, showUserDialogButton.getSelection());
        // PrefUtil.savePrefs();
        return true;
    }

    @Override
    @SuppressWarnings("restriction")
    protected IPreferenceStore doGetPreferenceStore() {
        return org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getPreferenceStore();
    }
}
